﻿using RpcModel;
using RpcSync.DAL;
using RpcSync.Model.DB;

namespace RpcSync.Collect.Collect
{
    internal class IdgeneratorCollect : IIdgeneratorCollect
    {
        private IIdgeneratorDAL _Idgenerator;
        public IdgeneratorCollect(IIdgeneratorDAL idgenerator)
        {
            this._Idgenerator = idgenerator;
        }

        public int GetWorkId(long serverId, long sysTypeId)
        {
            int workId = this._Idgenerator.GetWorkId(serverId);
            if (workId != 0)
            {
                return workId;
            }
            workId = this._Idgenerator.GetMaxWorkId(sysTypeId) + 1;
            _Idgenerator.Add(new IdgeneratorModel
            {
                ServerId = serverId,
                SystemTypeId = sysTypeId,
                WorkId = workId
            });
            return workId;
        }
    }
}
